package com.sqj.factoryPattern.simpleFactoryPattern;

import java.util.Objects;

public class SimpleFactory {
    public static ICourse createCourseOne(String courseName) {
        if (Objects.equals(courseName, "java")) {
            return new JavaCourse();
        } else if (Objects.equals(courseName, "python")) {
            return new PythonCourse();
        } else {
            return null;
        }
    }

    public static ICourse createCourseTwo(String className) {
        ICourse course = null;
        try {
            if (null != className) {
                course = (ICourse) Class.forName(className).newInstance();
                return course;
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
        return course;
    }

    public static ICourse createCourseThree(Class<? extends ICourse> clazz) {
        try {
            if (Objects.nonNull(clazz)) {
                return clazz.newInstance();
            }
        } catch (InstantiationException e) {
            e.printStackTrace();
        } catch (IllegalAccessException e) {
            e.printStackTrace();
        }
        return null;
    }
}
